<div ng-controller="articleMultiCtrl">
	<!-- search & list -->
	<div class="fade-animate" ng-show="mode(['search'])" lazy="search()">
		<!-- list -->
		<div waterfall data="artms" as="artm" num="4" start="1">
			<a href="#" class="upload-image" ng-click="add()"><b class="glyphicon glyphicon-plus"></b></a>
			<div class="fade-animate waterfall-elem" article-multi="artm">
				<a href="#" ng-click="del(artm, n)"><i class="fa fa-trash"></i> &{'app.common.button.del'}</a>
			</div>
		</div>
		<!-- /list -->
	</div>
	<!-- /search & data -->
	
	<!-- form -->
	#{include 'Resources/article_multi_form.html' /}
	<!-- /form -->
</div>

<script type="text/javascript">
'use strict'
var articleMultiCtrl = ['$scope', '$http', function($scope, $http) {
	// 定义方法
	angular.extend($scope, {
		init: function() {
			$scope.mode('search');
		}, btnDisabled: function() {
			return (angular.isUndefined($scope.article_multi) || $scope.article_multi.length <= 0 || $scope.btn(['doing']));
		}, search: function() {
			// 初始化当前页号
			if (angular.isUndefined($scope.artms_current)) $scope.artms_current = 1;
			
			// 仅当总页数为空或小于等于当前页数时进行加载数据
			if (angular.isUndefined($scope.artms_total) || $scope.artms_total >= $scope.artms_current) {
				$http({
					method: 'post',
					url: '@{Resources.article_multi_search}',
					data: jQuery.param({page: $scope.artms_current, size: 20}),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).success(function(data) {
					$scope.artms = data.content;
					$scope.artms_total = data.totalPage;
					$scope.artms_current = $scope.artms_current + 1;
				});
			}
		}, searchArticles: function() {
			// 初始化当前页号
			if (angular.isUndefined($scope.ac)) $scope.ac = 1;
			
			// 仅当总页数为空或小于等于当前页数时进行加载数据
			if (angular.isUndefined($scope.at) || $scope.at >= $scope.ac) {
				$http({
					method: 'post',
					url: '@{Resources.article_search}',
					data: jQuery.param({page: $scope.ac, size: 20}),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).success(function(data) {
					$scope.articles = data.content;
					$scope.at = data.totalPage;
					$scope.ac = $scope.ac + 1;
				});
			}
		}, add: function() {
			$scope.mode('add');
			$scope.btn('wait');
			$scope.article_multi = [];
			$scope.searchArticles();
		}, save: function() {
			var ids = [];
			angular.forEach($scope.article_multi, function(val, idx) { ids.push(val.id); });
			$http({
				method: 'post',
				url: '@{Resources.article_multi_save}',
				data: 'articles=' + ids.join(','),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
				$scope.artms_lane[1].splice(0, 0, data);
				$scope.mode('search');
			});
		}, del: function(art, idx) {
			$http({
				method: 'get',
				url: '@{Resources.article_multi_delete}?id=' + art.id,
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
				$scope.remove(art, $scope.artms_lane[idx]);
			});
		}, cancel: function() {
			$scope.mode('search');
			$scope.article_multi = [];
		}, select: function(article) {
			$scope.article_multi.push(article);
		}, notInSelected: function(article) {
			return (jQuery.inArray(article, $scope.article_multi) < 0);
		}
	});
	
	// 初始化
	$scope.init();
}];
</script>
